package cn.afterturn.oneopen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.afterturn.oneopen.model.AccountFlowModel;
import cn.afterturn.oneopen.model.ChargeModel;
import cn.afterturn.oneopen.model.OperationLogModel;
import cn.afterturn.oneopen.model.OrgInfoModel;
import cn.afterturn.oneopen.service.IAccountFlowService;
import cn.afterturn.oneopen.service.IOperationLogService;
import cn.afterturn.oneopen.service.IOrgInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * 从redis获取,入库
 *
 * @author by jueyue on 19-3-24.
 */
@Component
public class RedisService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisService.class);


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private IOperationLogService operationLogService;
    @Autowired
    private IOrgInfoService      orgInfoService;
    @Autowired
    private IAccountFlowService  accountFlowService;


    @Scheduled(cron = "0/2 * * * * ?")
    public void start() {
        boolean isHave = true;
        while (isHave) {

            try {
                OperationLogModel logModel  = handlerLog();
                ChargeModel       flowModel = handlerFlow();
                isHave = !(logModel == null && flowModel == null);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }

    private ChargeModel handlerFlow() {
        ChargeModel chargeModel = (ChargeModel) redisTemplate.opsForList().rightPop("CHARGE:REQUEST");
        if (chargeModel != null) {
            OrgInfoModel     orgInfoModel = orgInfoService.getOne(new QueryWrapper<OrgInfoModel>().eq("token", chargeModel.getToken()));
            AccountFlowModel flowModel    = new AccountFlowModel();
            flowModel.setAccountNo(orgInfoModel.getAccountNo());
            flowModel.setBalance(orgInfoModel.getBalance());
            flowModel.setCompleteTime(new Date());
            flowModel.setCurrency(1);
            // 这个价格看看怎么获取比较好
            flowModel.setMoney(1);
            flowModel.setTradeTime(new Date());
            flowModel.setTradeNo(UUID.randomUUID().toString().replace("-", ""));
            accountFlowService.save(flowModel);
            orgInfoService.updateBalance(orgInfoModel.getId(), -1 * flowModel.getMoney());
        }
        return chargeModel;
    }

    private OperationLogModel handlerLog() {
        OperationLogModel logModel = (OperationLogModel) redisTemplate.opsForList().rightPop("TIME:LOG");
        if (logModel != null) {
            OrgInfoModel orgInfoModel = orgInfoService.getOne(new QueryWrapper<OrgInfoModel>().eq("token", logModel.getAccountNo()));
            logModel.setAccountNo(orgInfoModel.getAccountNo());
            operationLogService.save(logModel);
        }
        return logModel;
    }
}
